package com.deep.deepenterpise.common.util;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @author xyz
 * @version 1.0
 * @title DrRandomUtil
 * @description
 * @create 2024/4/24 10:45
 */
public class DrRandomUtil {
    private static final String NUMBERS = "0123456789";
    private static final String UPPER_CASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
    private static final String ALL_CHARACTERS = NUMBERS + UPPER_CASE_LETTERS + LOWER_CASE_LETTERS;

    private static final Random RANDOM = new SecureRandom();

    public static String generateRandomString(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHARACTERS.charAt(RANDOM.nextInt(ALL_CHARACTERS.length())));
        }
        return sb.toString();
    }

    public static int generateRandomNumber(int min, int max) {
        if (min >= max) {
            throw new IllegalArgumentException("Max must be greater than min");
        }
        return RANDOM.nextInt(max - min + 1) + min;
    }

    public static int generateRandomNumber(int digits) {
        if (digits <= 0) {
            throw new IllegalArgumentException("Digits must be greater than zero");
        }
        int min = (int) Math.pow(10, digits - 1);
        int max = (int) Math.pow(10, digits) - 1;
        return RANDOM.nextInt(max - min + 1) + min;
    }

    public static void main(String[] args) {
        System.out.println(generateRandomString(16));
    }
}
